package models.common.entity;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.Transient;

import play.db.jpa.Model;

import common.constants.ConfConst;
import common.constants.Constants;
import common.utils.Security;
import common.utils.number.Arith;
/**
 * 保证金实体
 * @author liaijun
 * @createDate 2017/04/17
 */
@Entity
public class t_bond extends Model{

	/**退回金额*/
	public double out_money;
	/**剩余退回金额*/
	@Transient
	public double outMoney;
	
    public double getOutMoney () {
		return Arith.sub(have_send_money, out_money);
	}
	/**退回凭证地址*/
	public String out_pic_url;
	 /**
     *票号 
     */
    public String  bill_code;
    /**
     *凭证图片
     */
    public String  pic_url;
    /**
     * 用户id 
     */
    public long company_id;
    /**
     * 企业名称 
     */
    public String company_name;

    /**
     *放款企业id（企业id）
     */
    public long receive_company_id;
    
    /**
     *放款企业名称
     */
    public String receive_company_name;
    /**
     * 保证金总额
     */
    public double bond_total_money;

    /**
     *票面金额
     */
    public double listed_money;

    /**
      *保证金比例
     */
    public double bond_percent;
    
    /**
     * 已交保证金金额
     */
    public double have_send_money;
    
    /**剩余应缴纳保证金*/
    @Transient
    public double payMargin;
    public double getPayMargin(){
    	return Arith.sub(bond_total_money, have_send_money);
    }
    
    /**剩余应退还保证金*/
    @Transient
    public double backMargin;
    public double getBackMargin(){
    	return Arith.sub(bond_total_money, out_money);
    }
    /**
     * 保证协议 0未签署1已签署'
     */
    public int bond_agreement;
    
    /**
     * 授信申请表ID
     */
    public long commer_paper_id;
    /**
     *  添加时间
     */
    public Date create_time;
    
    /**是否放款，默认是 0 未放款，1 已放款*/
    public int is_loan;
    
    /**
     * 类型
     */
    public int type;
    
    /**
     * 保证金的状态状态0 未缴纳1 未交清2 审核中3 已缴纳4 已完成5 已退回
     * @mbggenerated
     */
    public int bond_status;

    public  Status getBondStatus()
    {
    	return Status.getStatusEnum(this.bond_status);
    }
    public  AgreeMent getAgreeMent(){
		return AgreeMent.getStatusEnum(this.bond_agreement);
    }
    
    
    public enum Status{
    	
    	/**
    	 * 未缴纳
    	 */
    	NO_PAY(0,"未缴纳"),
    	
    	/**
    	 *  待接收保证金
    	 */
    	UN_FINISHED(1,"待接收保证金"),
    	
    	/**
    	 * 已接收保证金
    	 */
    	AUDITING(2,"已接收保证金"),
    	
    	/**
    	 * 待退回
    	 */
    	WAIT_FOR_BACK(3,"待退回"),
    	/**
    	 * 已退回
    	 */
    	OK_FOR_BACK(4,"已退回保证金"),
    	/**
    	 * 已完成
    	 */
    	FINISHED(5,"已完成"),

        /**
         * 待接收保证金
         */
    	OK_FINISHED(8,"待接收保证金"),
    	
    	/**
    	 * 已通过
    	 */
    	HAVE_PAID(9,"已通过"),
    	/**
    	 * 未通过
    	 */
    	NO_PASSED(10,"未通过"),;
    	
    	public int code;
    	public String value;
    	
    	private Status(int code,String value)
    	{
    		this.code = code;
    		this.value = value;
    	}
    	
    	public static Status getStatusEnum(int code){
    		Status [] status = Status.values();
    		for (Status statu : status) {
				if (statu.code == code) {
					return statu;
				}
			}
    		return null;
    	}
    	
    }
    
    public enum AgreeMent{
    	
    	/**
    	 * 未签署承诺函
    	 */
    	NO(0,"未签署承诺函"),
    	
    	/**
    	 * 已签署承诺函
    	 */
    	OK(1,"已签署承诺函");
    	
    	public int code;
    	public String value;
    	
    	private AgreeMent(int code,String value)
    	{
    		this.code = code;
    		this.value = value;
    	}
    	
    	public static AgreeMent getStatusEnum(int code){
    		AgreeMent [] status = AgreeMent.values();
    		for (AgreeMent agre : status) {
				if (agre.code == code) {
					return agre;
				}
			}
    		return null;
    	}
    }
    /**加密ID*/
	@Transient
	public String sign;
	
    public String getSign () {
		return Security.addSign(this.id, Constants.BID_ID_SIGN, ConfConst.ENCRYPTION_KEY_DES);
	}
    /**加密BID_ID*/
  	@Transient
  	public String paperSign;
  	 public String getPaperSign () {
 		return Security.addSign(this.commer_paper_id, Constants.BID_ID_SIGN, ConfConst.ENCRYPTION_KEY_DES);
 	}
}